package com.starblink.search.result.ui.widget

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.extension.dp2px

/**
 *  Create by yuanl at 2023/4/17
 **/
class FilterPropertyDecoration(bgColor: Int, private val radius: Int = 50.dp2px()) :
    RecyclerView.ItemDecoration() {


    private val paint = Paint().apply {
        isAntiAlias = true
        color = bgColor
    }

    private val path = Path()

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(canvas, parent, state)

        path.reset()
        for (i in 0 until parent.childCount) {
            val view = parent.getChildAt(i)
            if (view.isSelected) {
                val leftF = view.left.toFloat()
                val topF = view.top.toFloat()
                val rightF = view.right.toFloat()
                val bottomF = view.bottom.toFloat()
                val centerY = topF + (bottomF - topF) / 2
                path.moveTo(leftF - radius.shr(1), bottomF)
                path.quadTo(leftF, bottomF, leftF, centerY)
                path.lineTo(rightF, centerY)
                path.quadTo(rightF, bottomF, rightF + radius.shr(1), bottomF)
                canvas.drawPath(path, paint)
            }
        }


    }

}